//
// Created by Huiyi on 2021/4/25.
//
#include "module_XExpand.h"

//取随机文本A
CXTextA X_String_GetRandStrA (int len)
{
    //26*2个英文字母+10个数字
    int maplen = 62;
    //码表
    const char* map = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    //根据长度提前申请内存
    char* retc = (char*)malloc(len+1);
    srand((unsigned)time(NULL));
    for (int i=0;i<len;i++)
    {
        int mapindex = rand() % maplen;
        //范围在maplen与1之间
        retc [i] = map [mapindex];
    }
    //不要忘记结束符
    retc [len] = '\0';
    CXTextA rets = retc;
    free(retc);
    return rets;
}

//到小写A
CXTextA X_String_ToLowerA(const char* str)
{
    int len = strlen(str)+1;
    char* lstr = (char*)malloc(len);
    for (int i=0;i<len;i++)
    {
        lstr [i] = tolower(str[i]);
    }
    CXTextA ret = lstr;
    free(lstr);
    return ret;
}

//到大写A
CXTextA X_String_ToUpperA(const char* str)
{
    int len = strlen(str)+1;
    char* lstr = (char*)malloc(len);
    for (int i=0;i<len;i++)
    {
        lstr [i] = toupper(str[i]);
    }
    CXTextA ret = lstr;
    free(lstr);
    return ret;
}

//分割文本A
int X_String_SplitWithStlA(CXTextA str, CXTextA pattern,CXVector<CXTextA>&rets)
{
    rets.clear();
    if ("" == str.get())
    {
        return 0;
    }
    string strs;
    strs.append(str.get()).append(pattern.get());
    size_t pos = strs.find(pattern);
    size_t size = strs.size();
    while (pos != string::npos)
    {
        CXTextA x = strs.substr(0,pos);
        rets.add(x);
        strs = strs.substr(pos+1,size);
        pos = strs.find(pattern);
    }
    return rets.size();
}

//取中间文本A
CXTextA X_String_GetMiddleTextA(const char* text,const char*frontstr,const char*backstr,int begin,bool ifnotcasesensitive)
{
    string l_text = text;
    string l_frontstr = frontstr;
    string l_backstr = backstr;
    //前面文本位置
    int l_f=0;
    //后面文本位置
    int l_b=0;
    //不区分大小写
    if (ifnotcasesensitive)
    {
        for (int i=0;i<l_text.length ();i++)
        {
            l_text [i] = tolower (l_text[i]);
        }
        for (int i=0;i<l_frontstr.length ();i++)
        {
            l_frontstr [i] = tolower (l_frontstr[i]);
        }
        for (int i=0;i<l_backstr.length ();i++)
        {
            l_backstr [i] = tolower (l_backstr[i]);
        }
    }
    l_f = l_text.find(frontstr,begin+1);
    if (l_f!=string::npos)
    {
        l_f += l_frontstr.length();
        l_b = l_text.find(l_backstr,l_f);
        if(l_b!=string::npos)
        {
            CXTextA l_ret = l_text.substr(l_f,l_b-l_f);
            return l_ret;
        }
    }
    return "";
}

//删首尾空A
CXTextA X_String_TrimA(CXTextA str)
{
    string l = str;
    if( !l.empty() )
    {
        l.erase(0, l.find_first_not_of(' '));
        l.erase(l.find_last_not_of(' ') + 1);
    }
    return l;
}

//子文本替换A
CXTextA X_String_SubreplaceA(string resource_str, string sub_str, string new_str,int begin,int MaxRep)
{
    int i=begin;
    string dst_str = resource_str.substr (begin,resource_str.length()-begin);
    string::size_type pos = 0;
    while((pos = dst_str.find(sub_str)) != string::npos)   //替换所有指定子串
    {
        if (MaxRep > 0 && i >= MaxRep)
        {
            break;
        }
        dst_str.replace(pos, sub_str.length(), new_str);
        i++;
    }
    return dst_str;
}

//倒找文本A
int X_String_RfindA(const char*source ,const char* match,int index)
{
    int reti = -1;
    int l_inde = index;
    if (l_inde==-1)
    {
        l_inde=strlen(source)-strlen(match);
    }
    for(int i=l_inde;i>=0;i--)
    {
        if(source[i]==match[0] && strncmp(source+i, match, strlen(match))==0)
        {
            reti = i;
            goto RET;
        }
    }
    RET:
    return reti;
}

//文本取左边A
CXTextA X_String_GetLeftA(const char*text,int num)
{
    string l(text);
    return l.substr (0,num);
}

//文本取右边A
CXTextA X_String_GetRightA(const char*text,int num)
{
    string l(text);
    return l.substr (l.length()-num,num);
}


